package cn.ubest.mvvmcore.base

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import cn.ubest.mvvmcore.R
import cn.ubest.mvvmcore.bean.ListLoadState
import cn.ubest.mvvmcore.databinding.IncludeListWithRefreshBinding
import cn.ubest.mvvmcore.databinding.IncludeListWithoutRefreshBinding
import cn.ubest.mvvmcore.databinding.IncludeToolbarListWithRefreshBinding
import cn.ubest.mvvmcore.databinding.IncludeToolbarListWithoutRefreshBinding
import cn.ubest.mvvmcore.interfaces.IEmptyView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.*
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshListener
import java.lang.reflect.ParameterizedType

/**
 * Created by maoqi on 2020/9/24.
 */
abstract class BaseListFragment<VM : BaseListViewModel<*>, B : ViewDataBinding,
        T : Any> : BaseFragment<VM, B>() {
    var mRootView: View? = null
    var mRvList: RecyclerView? = null
    var mSrlRefresh: SmartRefreshLayout? = null
    val mListAdapter by lazy {
        initAdapter()
    }

    abstract fun initAdapter(): BaseQuickAdapter<T, *>

    override fun initData(arguments: Bundle?) {
        //这里处理加载状态
        mViewModel.mListLiveData.observe(this, Observer {
            when (it.state) {
                ListLoadState.INIT_LOAD_END -> {
                    mListAdapter.replaceData(it.list as ArrayList<T>)
                    if (this is IEmptyView && !mListAdapter.hasEmptyView()) {
                        mListAdapter.setEmptyView(getEmptyView())
                    }
                }
                ListLoadState.INIT_LOAD_COMPLETE -> {
                    mListAdapter.setNewData(it.list as ArrayList<T>)
                    mListAdapter.loadMoreModule?.loadMoreComplete()
                    if (this is IEmptyView && !mListAdapter.hasEmptyView()) {
                        mListAdapter.setEmptyView(getEmptyView())
                    }
                }
                ListLoadState.LOAD_COMPLETE -> {
                    mListAdapter.addData(it.list as ArrayList<T>)
                    mListAdapter.loadMoreModule?.loadMoreComplete()
                }
                ListLoadState.LOAD_END -> {
                    mListAdapter.addData(it.list as ArrayList<T>)
                    mListAdapter.loadMoreModule?.loadMoreEnd()
                }
            }
        })
    }

    override fun initView(view: View) {
        mRootView = view
        mRvList = mRootView?.findViewById(R.id.rv_list)
        initList()

        mSrlRefresh = mRootView?.findViewById(R.id.srl_refresh)
        initRefresh()
    }

    open fun initList() {
        mRvList?.also {
            it.adapter = mListAdapter
            it.layoutManager = getLayoutManager()
        }

        mListAdapter.loadMoreModule?.isEnableLoadMoreIfNotFullPage = false
        if (this is OnLoadMoreListener) {
            mListAdapter.loadMoreModule?.isEnableLoadMore = true
            mListAdapter.loadMoreModule?.setOnLoadMoreListener(this)
        } else {
            mListAdapter.loadMoreModule?.isEnableLoadMore = false
        }

        if (this is OnItemClickListener) {
            mListAdapter.setOnItemClickListener(this)
        }

        if (this is OnItemChildClickListener) {
            mListAdapter.setOnItemChildClickListener(this)
        }

        if (this is OnItemLongClickListener) {
            mListAdapter.setOnItemLongClickListener(this)
        }

        if (this is OnItemChildLongClickListener) {
            mListAdapter.setOnItemChildLongClickListener(this)
        }
    }

    open fun getLayoutManager(): RecyclerView.LayoutManager {
        return LinearLayoutManager(activity)
    }

    open fun initRefresh() {
        mSrlRefresh?.also {
            if (this is OnRefreshListener) {
                it.setEnableRefresh(true)
                it.setOnRefreshListener(this)
            } else {
                it.setEnableRefresh(false)
            }
        }
    }

    override fun startLoading(isInit: Boolean) {
        if (isInit) {
            mSrlRefresh?.autoRefresh()
        } else {
            super.startLoading(isInit)
        }
    }

    override fun stopLoading() {
        super.stopLoading()
        mSrlRefresh?.finishRefresh()
    }
}